package com.tjbaobao.videoplayol.utils;

import android.content.Context;
import android.os.AsyncTask;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import com.google.gson.Gson;
import com.tjbaobao.framework.utils.OKHttpUtil;
import com.tjbaobao.framework.utils.RxJavaUtil;
import com.tjbaobao.framework.utils.Tools;
import com.tjbaobao.videoplayol.dialog.LoadingDialog;
import com.tjbaobao.videoplayol.message.request.BaseRequest;
import com.tjbaobao.videoplayol.message.response.BaseResponse;

/**
 *
 * Created by TJbaobao on 2017/11/23.
 */

@SuppressWarnings("WeakerAccess")
public abstract class BaseAsyncTask<Request extends BaseRequest,Response extends BaseResponse> {

    protected static final String ERROR = "error";
    protected static final String OK = "ok";
    private static final long loadBaseTime = 600*1;
    private boolean isShowDialog = true;
    protected Request mRequest ;
    protected LoadingDialog loadingDialog;

    private long lastTime = 0;
    private Context context ;
    public BaseAsyncTask(Context context , Request request)
    {
        mRequest = request;
        this.context = context;
    }

    private Animation animation ;
    private View viewAnim  ;
    public void setAnim(View view,int resAnim)
    {
        viewAnim = view ;
        animation = AnimationUtils.loadAnimation(context,resAnim);
        view.setAnimation(animation);
    }

    public void execute()
    {
        onPreExecute();
        RxJavaUtil.runOnIOToUI(new RxJavaUtil.RxTask<Response>(){
            @Override
            public Response onIOThreadBack() {
                return doInBackground();
            }

            @Override
            public void onUIThread(Response response) {
                onPostExecute(response);
            }
        });
    }

    protected void onPreExecute() {
        if(isShowDialog)
        {
            if(loadingDialog==null)
            {
                loadingDialog = new LoadingDialog(context);
            }
            loadingDialog.show();
        }
        lastTime = System.currentTimeMillis();
        if(animation!=null)
        {
            viewAnim.startAnimation(animation);
        }
    }

    protected Response doInBackground() {

        String url = ConstantUtil.GET_SERVER_URL;
        String data = new Gson().toJson(mRequest);
        Tools.printLog("访问连接:"+url+",发送内容:"+data);
        String resultStr = OKHttpUtil.doPost(url,data);
        Tools.printLog("获取到的数据:"+resultStr);

        long nowTime = System.currentTimeMillis();
        if(nowTime-lastTime<loadBaseTime)
        {
            Tools.sleep(loadBaseTime-(nowTime-lastTime));
        }
        if(resultStr==null)
        {
            return null;
        }
        else
        {
            return getResponse(resultStr);
        }
    }

    protected void onPostExecute(Response response) {
        if(animation!=null) {
            animation.cancel();
            if(viewAnim!=null)
            {
                viewAnim.clearAnimation();
            }
        }
        if(isShowDialog)
        {
            loadingDialog.dismiss();
        }

        if(response==null)
        {
            Tools.showToast("网络访问出错");
            onFail();
        }
        else
        {
            if(response.getResultCode()!=null)
            {
                if(!response.getMessage().equals(""))
                {
                    Tools.showToast(response.getMessage());
                    onFail();
                }
                if(response.getResultCode().equals(OK))
                {
                    onSuccess(response);
                }
            }
            else
            {
                onFail();
            }
        }
    }


    protected abstract Response getResponse(String resultStr);

    protected abstract void onSuccess(Response response);

    protected void onFail()
    {

    }
    protected void onError()
    {

    }

    public boolean isShowDialog() {
        return isShowDialog;
    }

    public void setShowDialog(boolean showDialog) {
        isShowDialog = showDialog;
    }
}
